﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Logic.UI;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public sealed class UIControlAlphaTrack : BaseTrack
    {
        [XmlElement("Alpha")]
        public string alpha = "";

        private Curve curveAlpha = null;

        public UIControlAlphaTrack() { }

        public UIControlAlphaTrack(string name) : base(name) { }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveAlpha = LoadCurve(contentManager, alpha);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            if (curveAlpha == null)
            {
                return false;
            }

            XUIPlaceableControl control = this.target as XUIPlaceableControl;
            if (control != null)
            {
                byte alpha = (byte)Math.Round(255 * curveAlpha.Evaluate(time));
                control.Alpha = alpha;
                return true;
            }

            return false;
        }
    }
}
